package com.eystar.ext.kit;

import java.io.IOException;

import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;

public class Ip2RegionKit {

	static DbSearcher searcher;
	static{
		try {
			DbConfig config = new DbConfig();
			String dbfile = Ip2RegionKit.class.getResource("/ip2region.db").getPath();
			searcher = new DbSearcher(config, dbfile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取IP解析的内容：中国|0|山东省|青岛市|阿里云
	 * @param ip
	 * @return
	 */
	public static String getBtreeRegion(String ip){
		DataBlock block;
		try {
			block = searcher.btreeSearch(ip);
			return block.getRegion(); 
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取IP解析的内容：中国|0|山东省|青岛市|阿里云, 内存方式方式，速度最快
	 * @param ip
	 * @return
	 */
	public static String getMemRegion(String ip){
		DataBlock block;
		try {
			block = searcher.memorySearch(ip);
			return block.getRegion(); 
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}
